package com.ymxx.jweb.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil {

	public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
	public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
	public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

	private DateTimeUtil() {}

	/**
	 * return current time eg 14:23:12
	 * 
	 * @return
	 */
	public static String getCurrentTime() {

		return date2Str(new Date(), DEFAULT_TIME_FORMAT);
	}

	/**
	 * return current date eg 2013-06-03
	 * 
	 * @return
	 */
	public static String getCurrentDate() {

		return date2Str(new Date(), DEFAULT_DATE_FORMAT);
	}

	/**
	 * return current datetime eg 2013-06-03 14:12:54
	 * 
	 * @return
	 */
	public static String getCurrentDatetime() {

		return date2Str(new Date(), DEFAULT_DATETIME_FORMAT);
	}

	/**
	 * @param date
	 * @param fmt
	 * @return
	 */
	public static String date2Str(Date date, String fmt) {

		if (date == null || fmt == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(fmt);
		return sdf.format(date);
	}

	/**
	 * @param date
	 * @return
	 */
	public static String date2Str(Date date) {

		return date2Str(date, DEFAULT_DATE_FORMAT);
	}

	/**
	 * @param dateStr
	 * @param fmt
	 * @return
	 */
	public static Date str2Date(String dateStr, String fmt) {

		if (dateStr == null || fmt == null) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(fmt);
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param dateStr
	 * @return
	 */
	public static Date str2Date(String dateStr) {

		return str2Date(dateStr, DEFAULT_DATE_FORMAT);
	}

	/**
	 * 返回相差的天数 忽略时间，保留日期
	 * 
	 * @param first
	 * @param second
	 * @return (<0说明first的日期在前面,>0说明second的日期在前面,==0说明是同一天)
	 * @throws ParseException
	 */
	public static int sub(Date first, Date second) {
		if (first == null || second == null) {
			throw new NullPointerException("时间参数为空");
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			first = sdf.parse(sdf.format(first));
			second = sdf.parse(sdf.format(second));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long _first = first.getTime();
		long _end = second.getTime();
		long day = ((_first - _end) / 1000 / 60 / 60 / 24);
		return (int) day;
	}

	/**
	 * @param date
	 * @return
	 */
	public static int getYear(java.util.Date date) {

		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.YEAR);
	}

	/**
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.MONTH) + 1;
	}

	/**
	 * @param date
	 * @return
	 */
	public static int getDay(Date date) {

		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date setDay(Date date, int day) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, day);
		return c.getTime();
	}

	/**
	 * 是否不早于当前日期（忽略时分秒）
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isGreaterEqualsCurDay(Date date) {
		int subCount = (int) sub(date, new Date());
		if (subCount < 0) {
			return true;
		}
		return false;
	}

	/**
	 * 判断日期是否在指定的区间内（忽略时分秒）
	 * 
	 * @param date
	 * @param start
	 * @param end
	 * @return
	 */
	public static boolean isBetween(Date date, Date start, Date end) {
		if (sub(date, start) > 0 && sub(date, end) < 0) {
			return true;
		}
		return false;
	}

	/**
	 * 以月份为单位，对日期进行按月加减操作
	 * 
	 * @param date
	 * @param num
	 *            (num<0表示从指定日期向前)
	 * @return
	 */
	public static Date calcDateOnMonth(Date date, int num) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, num);
		return calendar.getTime();
	}

	/**
	 * 以天为单位，对日期进行按天加减操作
	 * 
	 * @param date
	 * @param num
	 *            (num<0表示从指定日期向前)
	 * @return
	 */
	public static Date calcDateOnDay(Date date, int num) {
		Calendar calendar = null;
		try {
			calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_MONTH, num);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return calendar.getTime();
	}

	/**
	 * 判断是否是同一年同一月分
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static boolean isTheSameMonth(Date d1, Date d2) {
		if (d1 == null || d2 == null) {
			return false;
		}
		if (getYear(d1) == getYear(d2) && getMonth(d1) == getMonth(d2)) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否是同一天
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static boolean isTheSameDay(Date d1, Date d2) {
		if (d1 == null || d2 == null) {
			return false;
		}
		if (getYear(d1) == getYear(d2) && getMonth(d1) == getMonth(d2)
				&& getDay(d1) == getDay(d2)) {
			return true;
		}
		return false;
	}

	/**
	 * 获取指定日期所在月的第一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfMonth(Date date) {

		if (date == null)
			return null;
		Date firstDay = setDay(date, 1);
		return firstDay;
	}

	/**
	 * 获取指定日期所在月的第最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfMonth(Date date) {

		if (date == null)
			return null;
		Date first = setDay(date, 1);
		Date nextMonthFirstDay = calcDateOnMonth(first, 1);
		Date end = calcDateOnDay(nextMonthFirstDay, -1);
		return end;
	}
}
